package cn.kinoko.controller.cnTeam;

import cn.kinoko.common.aspect.annotation.CacheEvict;
import cn.kinoko.common.aspect.annotation.OwnershipCheck;
import cn.kinoko.common.base.model.BaseController;
import cn.kinoko.common.base.model.Result;
import cn.kinoko.common.base.validate.ValidGroup;
import cn.kinoko.common.constant.CacheKeyConstant;
import cn.kinoko.model.cnTeam.entity.CnTeam;
import cn.kinoko.model.cnTeam.query.CnTeamQuery;
import cn.kinoko.model.cnTeam.req.CnTeamPositionReq;
import cn.kinoko.model.cnTeam.req.CnTeamReq;
import cn.kinoko.model.cnTeam.vo.CnTeamPageVO;
import cn.kinoko.service.cnTeam.CnTeamService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author kinoko
 */
@RestController
@RequestMapping("/cnTeam")
public class CnTeamController extends BaseController {

    @Resource
    private CnTeamService cnTeamService;

    @PostMapping("/page")
    public Result<Page<CnTeamPageVO>> page(@RequestBody CnTeamQuery query) {
        return success(cnTeamService.queryPage(query));
    }

    @PostMapping("/save")
    public Result<Object> save(@RequestBody @Validated CnTeamReq req) {
        return toAjax(cnTeamService.save(req));
    }

    @OwnershipCheck(bizId = "#req.tid", bizClass = CnTeam.class)
    @PostMapping("/update")
    public Result<Object> update(@RequestBody @Validated({ValidGroup.Update.class}) CnTeamReq req) {
        return toAjax(cnTeamService.update(req));
    }

    @OwnershipCheck(bizId = "#tid", bizClass = CnTeam.class)
    @GetMapping("/deleted")
    public Result<Object> deleted(@RequestParam("tid") Long tid) {
        return toAjax(cnTeamService.deleted(tid));
    }

    @GetMapping("/dt")
    public Result<Object> detail(@RequestParam Long tid) {
        return success(cnTeamService.detail(tid));
    }

    @CacheEvict(key = CacheKeyConstant.JOIN_TEAM_KEY, currentUser = true)
    @GetMapping("/join")
    public Result<Object> joinTeam(@RequestParam Long tid, @RequestParam @NotBlank(message = "请输入邀请码") String joinCode) {
        return success(cnTeamService.joinTeam(tid, joinCode));
    }

    @GetMapping("/teamMember")
    public Result<Object> teamMember(@RequestParam Long tid) {
        return success(cnTeamService.teamMember(tid));
    }

    @GetMapping("/position")
    public Result<Object> position() {
        return success(cnTeamService.positionList());
    }

    @PostMapping("/changePosition")
    public Result<Object> changePosition(@RequestBody @Validated CnTeamPositionReq req) {
        return success(cnTeamService.changePosition(req));
    }

    @CacheEvict(key = CacheKeyConstant.JOIN_TEAM_KEY, params = "#uid")
    @OwnershipCheck(bizId = "#tid", bizClass = CnTeam.class)
    @GetMapping("/removeMember")
    public Result<Object> removeMember(@RequestParam Long tid, @RequestParam Long uid) {
        return success(cnTeamService.removeMember(tid, uid));
    }

    @CacheEvict(key = CacheKeyConstant.JOIN_TEAM_KEY, currentUser = true)
    @GetMapping("/quitTeam")
    public Result<Object> quitTeam(@RequestParam Long tid) {
        return success(cnTeamService.quitTeam(tid));
    }

    @GetMapping("/queryJoinTeam")
    public Result<Object> queryJoinTeam() {
        return success(cnTeamService.queryJoinTeamDdBox());
    }

}
